﻿namespace Lesson10_LinkedList;

class Program
{
    static void Main(string[] args)
    {
        #region 知识点一 基本概念
        //LinkList是c#为我们封装好的类
        //它本质是一个可变类型的泛型双向链表 
          #endregion
        
        #region  LinkedList 增删查改
        LinkedList<int> linkedList = new LinkedList<int>();
        #region 增
        linkedList.AddLast(10);
        linkedList.AddFirst(20);
        linkedList.AddLast(30);
        //在某个节点前添加一个元素
        // LinkedListNode<int> n = linkedList.Find(20);
        // linkedList.AddAfter(n, 12);
        // linkedList.AddAfter(n, 14);

        #endregion
        
        #region 删
        linkedList.RemoveFirst();   //删除头节点
        
        linkedList.RemoveLast();    //删除尾节点

        linkedList.Remove(10);      //删除指定元素

        linkedList.AddLast(30);
        linkedList.AddLast(40);
        linkedList.AddLast(50);
        linkedList.AddFirst(20);
        #endregion

        
        #region 查
        LinkedListNode<int> first = linkedList.First;  
        LinkedListNode<int> last = linkedList.Last;

        LinkedListNode<int> node = linkedList.Find(20);
        Console.WriteLine(node.Value);

        //判断指定元素是否存在并且输出
        if (linkedList.Contains(30))
        {
            Console.WriteLine("存在值为20的元素");
        }
        #endregion



        #region 循环遍历
        //foreach循环遍历
        foreach (var item in linkedList)
        {
            Console.WriteLine(item);
        }

        Console.WriteLine("*************************");
        //从头节点开始遍历
         LinkedListNode<int> NowHead = linkedList.First;
         while (NowHead != null)
         {
             Console.WriteLine(NowHead.Value);
             NowHead = NowHead.Next;
         }

        //从尾部开始循环遍历
        Console.WriteLine("**********************");
        LinkedListNode<int> NowTail = linkedList.Last;
        while (NowTail != null)
        {
            Console.WriteLine(NowTail.Value);
            NowTail = NowTail.Previous;
        }

        #endregion
        #endregion
    } 
}